import time

from flask import jsonify

from src import cache
from src.const.code import Code
from src.http.example import BlueprintHttpExample
from src.util.rs import Rs


@BlueprintHttpExample.route('/cached-time')
@cache.cached(timeout=60)
def cached_time():
    msg = "Hello with cache!" + time.strftime("%Y-%m-%d %H:%M:%S")
    return jsonify(Rs(
        Code.OK,
        msg,
    ).to_dict())


@BlueprintHttpExample.route('/cache-set-time')
def cache_set_time():
    cache.set("tt", time.time())
    return jsonify(Rs(
        Code.OK,
        Code.message(Code.OK),
    ).to_dict())


@BlueprintHttpExample.route('/cache-get-time')
def cache_get_time():
    cache.get("tt")
    return jsonify(Rs(
        Code.OK,
        Code.message(Code.OK),
        {
            "tt": cache.get("tt")
        }
    ).to_dict())


@BlueprintHttpExample.route('/cache-delete-time')
def cache_delete_time():
    cache.delete("tt")
    return jsonify(Rs(
        Code.OK,
        "缓存tt已删除"
    ).to_dict())


@BlueprintHttpExample.route('/cache-clear')
def cache_clear():
    cache.clear()
    return jsonify(Rs(
        Code.OK,
        "所有缓存已清除"
    ).to_dict())
